<?php
namespace Addons\Blocks\Controller;
class IndexController extends \Addon
{
    public function __construct()
    {
        parent::__construct();
        $this->blocks_type = dict("blocks_type");
        $this->model = BoxModel("addon_blocks");
        $this->blocks_image = BoxModel("addon_blocks_image");

    }

    /**
     * 区块管理
     * @return
     */
    public function index()
    {
        $list = $this->model->get("status=1", "10", "", "id desc");
        $data = $list['list'];
        $this->group_name = I('group_name');
        if ($this->group_name) {
            $BlocksGroup = BoxModel("addon_blocks_group")->get(array('group_name' => $this->group_name));
            $blocks_id = array();
            foreach ($BlocksGroup as $one) $blocks_id[] = $one['blocks_name'];
            foreach ($data as $k => $one) {
                if (in_array($one['name'], $blocks_id)) $data[$k]['checked'] = 'checked="checked"';
            }
        }
        $this->data = $data;
								//dump($data['pager']);
        $this->show_page($list['pager']);
        $this->tpl = I('tpl');
      //  dump( $this->tpl);
        $this->admin_display($this->tpl);
    }


    public function blocks_group()
    {
        $this->group_name = I('group_name');
        $this->data = BoxModel("addon_blocks_group")->get(array('group_name' => $this->group_name), "", "order_by");
        $this->admin_display();

    }

    public function edit_blocks_group()
    {
        $this->id = I('id');
        $this->data = BoxModel("addon_blocks_group")->where(array('id' => $this->id))->find();
        if (IS_POST) {
            BoxModel("addon_blocks_group")->where(array('id' => $this->id))->save(array('title' => I('title')));
            $this->jump("保存成功");
        }
        $this->admin_display();

    }

    public function save_blocks_group()
    {
        $where = array(
            'group_name' => I('group_name'),
            'blocks_name' => I('blocks_id'),
        );
        $r = BoxModel("addon_blocks_group")->where($where)->find();
        if ($r) {
            BoxModel("addon_blocks_group")->where($where)->delete();
        } else {
            $where['add_time'] = date("Y-m-d H:i:s");
            $where['title'] = I('title');
            BoxModel("addon_blocks_group")->add($where);
        }

    }

    function del_blocks_group()
    {
        $where = array(
            'id' => I('id'),
        );
        BoxModel("addon_blocks_group")->where($where)->delete();
        $this->jump("删除成功");
    }

    /**
     * 分类
     */
    public function add()
    {
        $this->blocks_name = $blocks_name = I('blocks_name');
        $this->data = $this->model->where(array('name' => $blocks_name))->find();
        $this->images_blocks_name = "blocks_" . $blocks_name;
        $tpl_list = BoxModel("system_template")->get();
        $tpl_list[] = array(
            'name' => 'diy_tpl',
            'title' => '自定义模板',
        );
        $this->tpl_list = $tpl_list;
        $this->admin_display();
    }

    public function blocks_image()
    {
					   $this->tpl=I('tpl',"image");
        $this->blocks_name = I('blocks_name');
        $data = $this->blocks_image->get("blocks_name='$this->blocks_name'", "10",  "order_by");
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        $this->admin_display("blocks_".$this->tpl);
    }

    public function del_blocks_image()
    {
        $this->tpl=I('tpl',"image");
					   $data = $this->blocks_image->where(array('id' => I('id')))->find();
        $this->blocks_name = $data['blocks_name'];
        $this->blocks_image->where(array('id' => I('id')))->delete();
        $this->jump("删除成功", addons_url('blocks_image', array('blocks_name' => $this->blocks_name,'tpl'=>$tpl)));
    }


    public function add_blocks_image()
    {
        $this->blocks_name = I('blocks_name');
								$this->tpl=I('tpl',"image");
        if (IS_POST) {
            $model = $this->blocks_image;
            $data = $model->create();
            $this->blocks_name = $data['blocks_name'];
            $data['store_id'] = $this->admin_store_id;
            $info = $this->_upload(array('savePath' => 'blocks_image'));
            if (is_array($info)) {
                $info = $info['_image_'];
                $data['image'] = $info['savepath'] . $info['savename'];
            }
            if ($id = I('id')) {
                $model->where(array('id' => $id))->save($data);
            } else {
                $data['add_time'] = date("Y-m-d H:i:s");
                $model->add($data);
            }
            $this->jump("保存成功", addons_url('blocks_image', array('blocks_name' => $this->blocks_name,'tpl'=>$this->tpl)));
        }
        if ($id = I('id')) {
            $this->data = $this->blocks_image->where(array('id' => $id))->find();
            $this->blocks_name = $this->data['blocks_name'];
        }
        $this->admin_display("add_blocks_".$this->tpl);
    }

    //广告组
    function showGroupHtml($p)
    {
        $group_name = $p['name'];
        $data = BoxModel("addon_blocks_group")->get(array('group_name' => $group_name), "", "order_by");
							 foreach ($data as $one) {
									   $this->params=$p;  
            $this->showHtml(array('name'=>$one['blocks_name']));
        }
    }


    //区块
    function showHtml($p)
    {
        $name = $p['name'];
        $this->data = $data = BoxModel("addon_blocks")->where("name='$name' and status=1")->find();
        if ($data['enable'] != 1) {//未启用
            return false;
        }
        //读取缓存
        $cacheKey = "addon_blocks_html_" . $name;
        $content = S($cacheKey);
        if ($content) {
            echo $content;
            return;
        }
        if ($data['type'] == 'none') {//原样输出
            $content = $data['tpl'];
            $content = stripslashes(htmlspecialchars_decode($content));
        } else if ($data['type'] == 'PHP') {//php程序
            $content = $data['tpl'];
            $tpl_content = stripslashes(htmlspecialchars_decode($content));
            if ($tpl_content) {
                $content = $this->fetch("", $tpl_content);
            }
        } else if ($data['type'] == 'image' || $data['type'] == 'topic') {//图片程序
            $blocks_name = "blocks_" . $data['name'];
            if ($data['tpl_type'] == "diy_tpl") {
                $tpl_content = $data['tpl'];
            } else {
                $r = BoxModel("system_template")->where(array('name' => $data['tpl_type']))->find();
                $tpl_content = $r['tpl_content'];
            }
            $blocks_image_list = BoxModel("addon_blocks_image")->get("blocks_name='$blocks_name' and status=1","","order_by");
												foreach($blocks_image_list as $k=>$one){
												 $blocks_image_list[$k]['content']=stripslashes(htmlspecialchars_decode($blocks_image_list[$k]['content']));
												}
												$this->blocks_image_list=$blocks_image_list;
            $tpl_content = stripslashes(htmlspecialchars_decode($tpl_content));
            if ($tpl_content) {
                $content = $this->fetch("", $tpl_content);
            }
        } else if ($data['type'] == 'widget'){//widget 程序
								   $content = $data['tpl'];
									  $widget=explode(",",$content);
											ob_start();
										 Widget($widget[0]."/".$widget[1]."/".$widget[2],$this->params);
										 $content=ob_get_contents();
											ob_end_clean();

										 
								}
        if ($data['cache'] == 1) {//缓存起来
            S($cacheKey, $content);
            BoxModel("addon_blocks")->set_cache($data['id'], $cacheKey);
        }
        echo $content;
    }

    //预览
    function viwe()
    {
        $this->name = I('name');
        $this->admin_display();
    }

    /**
     * 分类
     * @return [type] [description]
     */
    public function del()
    {
        if ($id = I('id')) {
            $this->model->delete($id);
            $this->jump("删除成功");
        }
    }

    public function add_do()
    {
        if (IS_POST) {
            $model = $this->model;
            $data = $model->create();
            $data['store_id'] = $this->admin_store_id;
												$data['tpl']= stripslashes(htmlspecialchars_decode($_POST['tpl']));
												if(I('type')=='widget'){
													$data['tpl']=strip_tags($data['tpl']);
												}
											//	dump($data);exit();
            if ($id = I('id')) {
                $model->where(array('id' => $id))->save($data);
            } else {
                $data['add_time'] = date("Y-m-d H:i:s");
                $id = $model->add($data);
            }
            $this->jump("操作成功", addons_url('add', array('blocks_name' => $data['name'])));
        }
    }


}